#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2017/11/2 13:14
# @Author  : Yunhao.Cao
# @File    : main.py
from __future__ import absolute_import, unicode_literals
from bs4 import BeautifulSoup
import requests

__author__ = 'Yunhao.Cao'

__ALL__ = []


def main():
    """
    解析页面：https://item.jd.com/3597549.html
    提取品牌名称，售价，累计评价数，店铺名称

    * CSS选择器
    :return:
    """
    root_url = "https://item.jd.com"
    detail_url = root_url + "/3597549.html"

    # 获取soup
    response = requests.get(detail_url)
    soup = BeautifulSoup(response.text, "html.parser")

    print soup.prettify().encode('gb18030')

    try:
        # 店铺名
        shop_name = soup.select_one("div.name a").get("title", None)
        print "shop name = {}".format(shop_name)

        # price
        price_text = soup.select_one("span.p-price span[class^=\'price J-p-\']").text
        print "good price = {}".format(price_text)

        # 评论数目
        comment_number_text = soup.select_one(
            "#detail > div.tab-main.large > ul > li[data-anchor=\'#comment\'] > s").text.replace("(", "").replace(")",
                                                                                                                  "")
        print "number of comment = {}".format(comment_number_text)

    except Exception as e:
        print("解析失败. e = {}".format(e))


if __name__ == '__main__':
    main()
